# -*- coding:utf8 -*-
from .. import db
from flask_login import UserMixin

class Tag(db.Model):
    __tablename__ = "tags"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128),unique=True, index=True)
    content = db.Column(db.UnicodeText)
    status = db.Column(db.Integer,default=0,nullable=False)

    # 视图渲染Tag列表和URL时所有tagurl都加载一次,频度太高,不必要的换Ajax?
    urls = db.relationship("TagUrl", order_by="TagUrl.id", backref="tag")
    #同在item中定义效果相同
    items = db.relationship("Item", secondary="item_tags", backref="tags")
    def __repr__(self):
        return '<Tag %r>' % (self.name)

class Category(db.Model):
    __tablename__ = "categories"
    id = db.Column(db.Integer, primary_key=True)
    category_name = db.Column(db.String(128),unique=True, index=True)
    #the same results with define in Item : add Item.category
    #items = relationship("Item", order_by="item.id", backref="category")
    def __repr__(self):
        return '<Category %r>' % (self.category_name)

class Item(db.Model):
    __tablename__ = "items"
    id = db.Column(db.Integer, primary_key=True)
    category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
    #double side relation:1 to n : Item.category = Category.items[]
    #并未搞懂怎样就关联到了Category对象 它怎么知道我要用category_id去找?
    category = db.relationship("Category", backref=db.backref('items', order_by=id, lazy="joined"))

    #tags = db.relationship("Tag", order_by="item_tags.item_id", backref="item")
    #在Tag中定义了 两边定义任何一个即可
    #tags = db.relationship("Tag", secondary="item_tags", backref=db.backref('item', lazy='dynamic') )
    title = db.Column(db.String(512))
    content = db.Column(db.UnicodeText) 
    readme = db.Column(db.UnicodeText) #Mark Down

    description = db.Column(db.String(512)) #rename to title.to be delete
    sample = db.Column(db.UnicodeText) #rename to readme. to be delete
    
    votes = db.Column(db.Integer)
    devotes = db.Column(db.Integer)
    create_by = db.Column(db.String)
    create_time = db.Column(db.DateTime)
    modified_by = db.Column(db.String)
    modified_time = db.Column(db.DateTime)
    owner = db.Column(db.String)
    status = db.Column(db.Integer)

    def __repr__(self):
        return '<Item %r>' %(self.content)

class TagUrl(db.Model):
    __tablename__ = "tag_urls"
    id = db.Column(db.Integer, primary_key=True)
    tag_id = db.Column(db.Integer,db.ForeignKey("tags.id"))
    url = db.Column(db.String(512))
    title = db.Column(db.String(512))

    #tag = db.relationship("Tag", backref=db.backref("urls",lazy="dynamic"))
    def __repr__(self):
        return '<TagUrl %r on %r>' %(self.title,self.tag_id)

class ItemComment(db.Model):
    __tablename__ = "item_comments"
    id = db.Column(db.Integer, primary_key=True)
    item_id = db.Column(db.Integer)
    comment = db.Column(db.UnicodeText)
    post_by = db.Column(db.Integer)
    post_time = db.Column(db.DateTime)

    def __repr__(self):
        return '<%s Comment on item %r>' %(self.post_by,self.item_id)

class ItemVote(db.Model):
    __tablename__ = "item_votes"
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer)
    item_id = db.Column(db.Integer)
    vote = db.Column(db.SmallInteger)
    vote_by = db.Column(db.Integer)
    vote_time = db.Column(db.DateTime)

    def __repr__(self):
        return '<%s Votes %r>' %(self.user_id,self.vote)

assoc_item_tags = db.Table('item_tags', #Base.metadata,
    db.Column('item_id', db.Integer, db.ForeignKey('items.id')),
    db.Column('tag_id', db.Integer, db.ForeignKey('tags.id'))
)

class ItemTags(db.Model):
    __tablename__ = "item_tags"
    __table_args__ = {"useexisting": True}
    id = db.Column(db.Integer, primary_key=True)
    #item = db.relationship("Item", backref=db.backref('tags', order_by=id, lazy="joined"))
    #item_id = db.Column(db.Integer,db.ForeignKey("items.id"))
    #tag_id = db.Column(db.Integer,db.ForeignKey("tags.id"))
    tagged_by = db.Column(db.Integer)
    tagged_time = db.Column(db.DateTime)

    def __repr__(self):
        return '<%s Item-Tag %r>' %(self.tag_id,self.item_id)
